package com.wangyan.javabean2doc.ui;

import com.intellij.util.Function;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.ListTableModel;
import com.intellij.util.ui.table.TableModelEditor;
import com.wangyan.javabean2doc.RowData;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import java.util.Comparator;

/**
 * @author wangyan
 * @date 2024/01/29/11:49
 * @description
 */
public class MyTable {
    private TableModelEditor<RowData> browsersEditor;

    private JComponent browsersTable;

    public JComponent getBrowsersTable() {
        return browsersTable;
    }


    private static final TableModelEditor.EditableColumnInfo<RowData, String> selectOptColumn = new TableModelEditor.EditableColumnInfo<RowData, String>("required") {
        @Override
        public Class getColumnClass() {
            return String.class;
        }

        @Override
        public String valueOf(RowData item) {
            return item.getRequired();
        }

        @Override
        public void setValue(RowData item, String value) {
            item.setRequired(value);
        }
    };

    private static final TableModelEditor.EditableColumnInfo<RowData, String> nameColumn = new TableModelEditor.EditableColumnInfo<RowData, String>("Name") {
        @Override
        public String valueOf(RowData item) {
            return item.getName();
        }

        @Override
        public void setValue(RowData item, String value) {
            item.setName(value);
        }

        @NotNull
        @Override
        public Comparator<RowData> getComparator() {
            return new Comparator<RowData>() {
                @Override
                public int compare(RowData o1, RowData o2) {
                    if (o1.getName().compareTo(o2.getName()) > 0) {
                        return 1;
                    } else if (o1.getName().compareTo(o2.getName()) == 0) {
                        return 0;
                    } else {
                        return -1;
                    }
                }
            };
        }
    };


    private static final ColumnInfo[] COLUMNS = {
            selectOptColumn,
            nameColumn};


    public MyTable() {
        TableModelEditor.DialogItemEditor<RowData> itemEditor = new TableModelEditor.DialogItemEditor<RowData>() {
            @NotNull
            @Override
            public Class<RowData> getItemClass() {
                return RowData.class;
            }

            @Override
            public RowData clone(@NotNull RowData item, boolean forInPlaceEditing) {
                return item;
            }

            @Override
            public void edit(@NotNull RowData item, @NotNull Function<? super RowData, ? extends RowData> mutator, boolean isAdd) {

            }

            @Override
            public void applyEdited(@NotNull RowData oldItem, @NotNull RowData newItem) {
            }

            @Override
            public boolean isEditable(@NotNull RowData browser) {
                return false;
            }


        };

        TableModelEditor.DataChangedListener<RowData> dataChangedListener = new TableModelEditor.DataChangedListener<RowData>() {
            @Override
            public void tableChanged(@NotNull TableModelEvent event) {
            }

            @Override
            public void dataChanged(@NotNull ColumnInfo<RowData, ?> columnInfo, int rowIndex) {
            }
        };

        browsersEditor = new TableModelEditor<>(COLUMNS, itemEditor, "No web browsers configured");
        browsersEditor.modelListener(dataChangedListener);

        ListTableModel<RowData> model = this.browsersEditor.getModel();
        RowData RowData1 = new RowData(
        );
        RowData1.setName("111");
        RowData RowData2 = new RowData(
        );
        RowData2.setName("222");

        model.addRow(RowData1);
        model.addRow(RowData2);

        browsersTable = browsersEditor.createComponent();
    }

}